<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="投诉人姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入投诉人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投诉人联系方式" prop="userLxfs">
        <el-input
          v-model="queryParams.userLxfs"
          placeholder="请输入投诉人联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="被投诉职位" prop="position">
        <el-select v-model="queryParams.position" placeholder="请选择职位" clearable>
          <el-option label="售前" value="售前" />
          <el-option label="售中" value="售中" />
          <el-option label="售后" value="售后" />
          <el-option label="技术" value="技术" />
        </el-select>
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable>
           <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['quick:complaint:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['quick:complaint:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['quick:complaint:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="complaintList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="投诉人" align="center" prop="userName" />
      <el-table-column label="系统联系方式" align="center" prop="userLxfs" width="120" />
      <el-table-column label="用户指定联系方式" align="center" prop="userContact" width="120"/>
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="投诉内容" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewContent(scope.row)"
          >查看详情</el-button>
        </template>
      </el-table-column>
      <el-table-column label="被投诉职位" align="center" prop="position" />
      <el-table-column label="被投诉人/事" align="center" prop="positionName" />
      <el-table-column
        label="处理状态"
        align="center"
        prop="status"
        :formatter="statusFormatter">
      </el-table-column>
      <el-table-column label="解决方案" align="center" width="180">
        <template slot-scope="scope">
           <div v-if="scope.row.solution">
             <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleViewSolution(scope.row)"
            >查看详情</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleProcess(scope.row)"
              v-hasPermi="['quick:complaint:edit']"
            >编辑</el-button>
          </div>
          <el-button
            v-else
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleProcess(scope.row)"
            v-hasPermi="['quick:complaint:edit']"
          >去处理</el-button>
        </template>
      </el-table-column>
       <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['quick:complaint:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改统一投诉对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="投诉人姓名" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入投诉人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投诉人系统联系方式" prop="userLxfs">
              <el-input v-model="form.userLxfs" placeholder="请输入投诉人联系方式" />
            </el-form-item>
          </el-col>
        </el-row>
         <el-row>
          <el-col :span="12">
            <el-form-item label="投诉订单ID" prop="orderId">
              <el-input v-model="form.orderId" placeholder="请输入投诉的订单id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投诉订单号" prop="orderNo">
              <el-input v-model="form.orderNo" placeholder="请输入投诉的订单号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="被投诉职位" prop="position">
                <el-select v-model="form.position" placeholder="请选择职位">
                    <el-option label="售前" value="售前" />
                    <el-option label="售中" value="售中" />
                    <el-option label="售后" value="售后" />
                    <el-option label="技术" value="技术" />
                </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="用户指定联系方式" prop="userContact">
                <el-input v-model="form.userContact" placeholder="用户填写的联系方式" />
                </el-form-item>
            </el-col>
        </el-row>
        <el-row>
             <el-col :span="12">
                <el-form-item label="被投诉人/事" prop="positionName">
                <el-input v-model="form.positionName" placeholder="请输入职位对应的名字" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="被投诉人/事ID" prop="positionId">
                <el-input v-model="form.positionId" placeholder="请输入职位对应的id" />
                </el-form-item>
            </el-col>
        </el-row>

        <el-form-item label="投诉内容" prop="content">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>

        <el-form-item label="解决方案" prop="solution">
          <editor v-model="form.solution" :min-height="192"/>
        </el-form-item>
        <el-form-item label="处理状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态" style="width:100%">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 处理投诉对话框 -->
    <el-dialog :title="processTitle" :visible.sync="processOpen" width="700px" append-to-body>
      <el-form ref="processForm" :model="processForm" :rules="processRules" label-width="80px">
        <el-form-item label="解决方案" prop="solution">
          <editor v-model="processForm.solution" :min-height="192"/>
        </el-form-item>
        <el-form-item label="处理状态" prop="status">
          <el-select v-model="processForm.status" placeholder="请选择状态" style="width:100%">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitProcessForm">确 定</el-button>
        <el-button @click="cancelProcess">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看内容详情对话框 -->
    <el-dialog :title="contentViewTitle" :visible.sync="contentViewOpen" width="1500px" append-to-body>
      <div v-html="currentContentView"></div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="contentViewOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listComplaint, getComplaint, delComplaint, addComplaint, updateComplaint } from "@/api/system/complaint"
import Editor from '@/components/Editor'

export default {
  name: "Complaint",
  components: { Editor },
  dicts: ['complaint_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 统一投诉表格数据
      complaintList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 内容详情
      contentViewOpen: false,
      contentViewTitle: "",
      currentContentView: "",
      // 状态选项
      statusOptions: [
        { value: 0, label: '待处理' },
        { value: 1, label: '处理中' },
        { value: 2, label: '已解决' },
        { value: 3, label: '已关闭' },
        { value: 4, label: '已驳回' }
      ],
      // 处理投诉弹窗
      processOpen: false,
      processTitle: "",
      processForm: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        userLxfs: null,
        orderNo: null,
        position: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "投诉人姓名不能为空", trigger: "blur" }
        ],
        userLxfs: [
          { required: true, message: "投诉人联系方式不能为空", trigger: "blur" }
        ],
        orderNo: [
          { required: true, message: "投诉的订单号不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "投诉内容不能为空", trigger: "blur" }
        ],
        position: [
          { required: true, message: "被投诉职位不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "处理状态不能为空", trigger: "blur" }
        ],
      },
      // 处理表单校验
      processRules: {
        solution: [
          { required: true, message: "解决方案不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "处理状态不能为空", trigger: "change" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {

    statusFormatter(row, column, cellValue) {
      const statusMap = {
        0: '待处理',
        1: '处理中',
        2: '已解决',
        3: '已关闭',
        4: '已驳回'
      };
      return statusMap[cellValue];
    },
    /** 查询统一投诉列表 */
    getList() {
      this.loading = true
      listComplaint(this.queryParams).then(response => {
        this.complaintList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        userLxfs: null,
        orderId: null,
        orderNo: null,
        content: null,
        position: null,
        positionName: null,
        positionId: null,
        userContact: null,
        status: 0,
        solution: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加统一投诉"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getComplaint(id).then(response => {
        this.form = response.data
        if(this.form.content){
          this.form.content = decodeURIComponent(this.form.content)
        }
        if(this.form.solution){
          this.form.solution = decodeURIComponent(this.form.solution)
        }
        this.open = true
        this.title = "处理投诉"
      })
    },
    /** 查看详情按钮操作 */
    handleViewContent(row) {
      this.contentViewOpen = true
      this.contentViewTitle = "投诉内容详情"
      this.currentContentView = row.content ? decodeURIComponent(row.content) : ''
    },
    /** 查看解决方案详情按钮操作 */
    handleViewSolution(row) {
      this.contentViewTitle = "解决方案详情";
      this.currentContentView = row.solution ? decodeURIComponent(row.solution) : '';
      this.contentViewOpen = true;
    },
    /** 处理投诉按钮操作 */
    handleProcess(row) {
      this.resetProcessForm();
      const complaintId = row.id || this.ids[0];
      getComplaint(complaintId).then(response => {
        this.processForm.id = response.data.id;
        this.processForm.status = response.data.status;
        this.processForm.solution = response.data.solution ? decodeURIComponent(response.data.solution) : '';
        this.processOpen = true;
        this.processTitle = "处理投诉";
      });
    },
    // 取消处理投诉按钮
    cancelProcess() {
      this.processOpen = false;
      this.resetProcessForm();
    },
    // 重置处理表单
    resetProcessForm() {
      this.processForm = {
        id: null,
        solution: null,
        status: null
      };
      this.resetForm("processForm");
    },
    /** 提交处理表单 */
    submitProcessForm() {
      this.$refs["processForm"].validate(valid => {
        if (valid) {
          const formData = { ...this.processForm };
          if (formData.solution) {
            formData.solution = encodeURIComponent(formData.solution);
          }
          updateComplaint(formData).then(response => {
            this.$modal.msgSuccess("处理成功");
            this.processOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const formData = { ...this.form };
          if (formData.content) {
            formData.content = encodeURIComponent(formData.content);
          }
          if (formData.solution) {
            formData.solution = encodeURIComponent(formData.solution);
          }
          if (formData.id != null) {
            updateComplaint(formData).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addComplaint(formData).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除统一投诉编号为"' + ids + '"的数据项？').then(function() {
        return delComplaint(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('quick/complaint/export', {
        ...this.queryParams
      }, `complaint_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
